<template>
  <el-date-picker
    v-model="dateData"
    :type="datePickerType"
    align="left"
    unlink-panels
    range-separator="至"
    start-placeholder="开始月份"
    end-placeholder="结束月份"
    value-format="yyyy-MM-dd"
    :disabled="disabled"
    :picker-options="
      datePickerType === 'date' ? DatePickerOptions : datedateRangePickerOptions
    "
  >
  </el-date-picker>
</template>

<script>
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false,
    },
    datePickerType: {
      type: String,
      default: "date",
    },
    itemObj: {
      type: Object,
      default: () => {
        return {};
      },
    },
    value: {},
    row: {
      type: Object,
      default: () => {
        return {
          test: "2023-07-23",
        };
      },
    },
    low: {
      type: String,
      default: "",
    },
    up: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      datedateRangePickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  computed: {
    dateData: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
    DatePickerOptions() {
      return {
        disabledDate: (time) => {
          if (this.low && this.row[this.low]) {
            // 入职时间选择后，禁用入职时间前的时间
            if (this.row[this.low].includes("T")) {
              return time.getTime() < new Date(this.row[this.low]);
            } else {
              return (
                time.getTime() < new Date(this.row[this.low] + "T00:00:00")
              );
            }
          }
          if (this.up && this.row[this.up]) {
            // 离职时间选择后，禁用离职时间后的时间
            if (this.row[this.up].includes("T")) {
              return time.getTime() > new Date(this.row[this.up]);
            } else {
              return time.getTime() > new Date(this.row[this.up] + "T00:00:00");
            }
          }
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      };
    },
  },
  watch: {},
};
</script>